package utils;

import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.deep_tools.ecdc.R;

import pl.droidsonroids.gif.GifImageView;

public class ToastUtils {
    private ToastUtils_CALLBACK mToastUtils_CALLBACK;

    public interface ToastUtils_CALLBACK {
        public void callBackToastUtils(String enterOrCancel);

    }

    private volatile static ToastUtils mToastUtils;
    private Toast toast;
//    private AutoResizeTextView textView;

    private ToastUtils() {
    }

    ;

    public static ToastUtils getToastUtilsInstance() {
        if (mToastUtils == null) {
            synchronized (ToastUtils.class) {
                if (mToastUtils == null) {
                    mToastUtils = new ToastUtils();
                }
            }
        }
        return mToastUtils;
    }

//    public void toastCenter(String content, Activity activity, int colorValus) {
//        if (toast != null) {
//            toast.cancel();
//            toast = null;
//            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);
//        } else {
//            textView = new AutoResizeTextView(activity);
//            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);
//        }
//
//        textView.setText(content);
////		textView.setWidth(300);
////		textView.setHeight(300);
//        textView.setBackgroundResource(R.drawable.toast_bacground);
//        textView.setGravity(Gravity.CENTER);
//        textView.setTextColor(colorValus);
//        toast.setGravity(Gravity.CENTER, 0, 0);
//        toast.setView(textView);
//        toast.show();
//    }

    @SuppressLint("ResourceType")
    public void toastCenterStrip(String content, Activity activity, int colorValus) {
        if (toast != null) {
            toast.cancel();
            toast = null;
            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);
        } else {
            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);

        }

        LinearLayout ll = (LinearLayout) toast.getView();
        TextView tv = ll.findViewById(android.R.id.message);
        tv.setPadding(20, 0, 20, 0);
        tv.setTextColor(colorValus);

        ll.setBackgroundResource(R.drawable.toast_bacground);

        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @SuppressLint("ResourceType")
    public void toastGif(String content, Activity activity, int colorValus) {
        if (toast != null) {
            toast.cancel();
            toast = null;
            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);
        } else {
            toast = Toast.makeText(activity, content, Toast.LENGTH_LONG);

        }

//        LinearLayout ll = (LinearLayout) toast.getView();
//        TextView tv = ll.findViewById(android.R.id.message);

        View view = LayoutInflater.from(activity).inflate(R.layout.view_gif, null);
        GifImageView dd = view.findViewById(R.id.ddddddd);
        toast.setView(view);


//        tv.setPadding(20, 0, 20, 0);
//        tv.setTextColor(colorValus);
//        ll.setBackgroundResource(R.drawable.toast_bacground);

        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    public void setToastUtilsDeligate(ToastUtils_CALLBACK mToastUtils_CALLBACK) {
        this.mToastUtils_CALLBACK = mToastUtils_CALLBACK;
    }

    public void showNormalDialog(Activity activity, String title, String message, String leftButtonContent, String rightButtonContent, int leftButtonColor, int rightButtonColor) {
        AlertDialog dialog = new AlertDialog.Builder(activity).setTitle(title).setMessage(message).setPositiveButton(rightButtonContent, new OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                mToastUtils_CALLBACK.callBackToastUtils("右边按钮");

            }
        }).setNegativeButton(leftButtonContent, new OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                mToastUtils_CALLBACK.callBackToastUtils("左边按钮");

            }
        }).create();
        dialog.show();
        // 通过反射设置文字颜色
        try {

            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object mAlertController = mAlert.get(dialog);

            Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
            mMessage.setAccessible(true);
            TextView mMessageView = (TextView) mMessage.get(mAlertController);
            mMessageView.setTextColor(Color.BLACK);
            // 更改标题的颜色
            // Field mTitleView1 =
            // mAlertController.getClass().getDeclaredField("mTitleView");
            // mTitleView1.setAccessible(true);
            // TextView tvTitle = (TextView) mTitleView1.get(mAlertController);
            // tvTitle.setTextColor(Color.GREEN);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        // 设置dialog按钮颜色
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(leftButtonColor);
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(rightButtonColor);

    }

    private Dialog loadingDialog;

    @SuppressLint("ResourceType")
    public void displayProgressDialog(Activity activity) {

        loadingDialog = new Dialog(activity, ProgressDialog.STYLE_HORIZONTAL);// 创建自定义样式dialog
        View view = LayoutInflater.from(activity).inflate(R.layout.view_gif, null);
        GifImageView dd = view.findViewById(R.id.ddddddd);
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消





        WindowManager.LayoutParams params = loadingDialog.getWindow().getAttributes();
        params.height = 233;
        params.width = 233;
        params.gravity = Gravity.CENTER;
        loadingDialog.getWindow().setAttributes(params);
        loadingDialog.getWindow().setBackgroundDrawableResource(R.drawable.circle_style8);


        loadingDialog.setContentView(view);
        loadingDialog.show();

    }

    public void dismissProgressDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }

    }

}
